package com.kfh.ybracelet.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import de.greenrobot.event.EventBus;

/**
 * 自定义聊天内容的父view
 * 该view有且只有有两个直接的子类, 第一个是RelativeLayout,第二个是listview
 * 用于在俩天页面下拉加载更多的聊天数据
 * Created by Aaron on 16/1/7.
 */
public class ChatView extends RelativeLayout {

    private float startY;
    private float moveY;
    private int changeY;
    private RelativeLayout loadingView;
    private ListView listView;
//    private int imageViewHeight;

    //查询更多聊天记录的code
    public static final int LOAD_MORE_CHAT_HISTORY = 0x61;

    private Context ctx;
    private int ivMeaHight;
    private int ivMeaWidth;
    private int lvMeaHeight;
    private ImageView circleView;
    private AnimationDrawable animationDrawable;
    //    private int listViewBottom;

    public ChatView(Context context) throws Exception {
        this(context, null);
    }

    public ChatView(Context context, AttributeSet attrs) throws Exception {
        this(context, attrs, 0);
    }

    public ChatView(Context context, AttributeSet attrs, int defStyleAttr) throws Exception {
        super(context, attrs, defStyleAttr);
        this.ctx = context;

        if (getChildCount() > 2)
            throw new Exception("最多只能有两个直接子类");
    }

    /**
     * 当这个view和其子view被分配一个大小和位置时，被layout调用。
     *
     * @param changed 当前View的大小和位置改变了
     * @param left    左部位置（相对于父视图
     * @param top     顶部位置（相对于父视图
     * @param right   右部位置（相对于父视图
     * @param bottom  底部位置（相对于父视图
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

//        if (changed)
//            Log.e("tag", "====onLayout====" + changed + ",l" + left + ",t" + top
//                    + ",r" + right + ",b" + bottom);

        loadingView = (RelativeLayout) getChildAt(0);
        //获取到ListView
        listView = (ListView) getChildAt(1);

        //imageview的高度
        ivMeaHight = loadingView.getMeasuredHeight();
        //imageview的宽度
        ivMeaWidth = loadingView.getMeasuredWidth();
        //listview的高度
        lvMeaHeight = listView.getMeasuredHeight();

        //所填的参数都是 子控件相对于父控件的距离
        loadingView.layout(0, -ivMeaHight, ivMeaWidth, 0);
//        listView.layout(0, 0, ivMeaWidth, lvMeaHeight);
        listView.layout(0, 0, ivMeaWidth, lvMeaHeight);

//        LogUtil.e("tag", "聊天listview的高度:" + listView.getHeight());

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //获取到手指按下时的y轴的偏移量
                startY = event.getY();

//                LogUtil.e("tag", "  ChatView  dispatchTouchEvent  down  ");

                break;

            case MotionEvent.ACTION_MOVE:

                moveY = event.getY();

//                LogUtil.e("tag", "  ChatView  dispatchTouchEvent  move  ");

                //滑动时Y轴方向的改变量
                changeY = (int) (moveY - startY);

                if (changeY > 20) {

                    circleView = (ImageView) loadingView.getChildAt(0);
                    animationDrawable = (AnimationDrawable) circleView.getDrawable();
                    animationDrawable.start();

                }

                //防止滑到最上面时 倒滑事件被截断
                if (changeY < 0) {
                    return super.dispatchTouchEvent(event);
                }

                //先判断当前的listview是否在顶部

                if (isFirstItemFullShow()) {
                    //如果第一个可见条目是 0 则处理下拉操作

                    //当y轴方向的偏移量大于0,并且小于等于imageview的高度时,
                    //让imageview和listview往下滑动
//                    if (changeY > 0) {
                    if (changeY > 0 && changeY <= ivMeaHight) {
                        loadingView.layout(0, changeY - ivMeaHight, ivMeaWidth, changeY);
                        listView.layout(0, changeY, ivMeaWidth, lvMeaHeight + changeY);
                    }

                    return true;

                }

//                Log.e("tag", "changeY:" + changeY);

                break;
            case MotionEvent.ACTION_UP:

//                LogUtil.e("tag", "  ChatView  dispatchTouchEvent  up  ");

                if (isFirstItemFullShow()) {

                    if (changeY > 100)
//                        Log.e("tag", "  刷新页面  ");
                        EventBus.getDefault().post(LOAD_MORE_CHAT_HISTORY);

                }

                //让位置还原
                loadingView.layout(0, -ivMeaHight, ivMeaWidth, 0);
                listView.layout(0, 0, ivMeaWidth, lvMeaHeight);

                if (animationDrawable != null)
                    animationDrawable.stop();

                break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * listview的第一个条目是否完全可见
     *
     * @return true 第一个条目可见, false 第一个条目不可见
     */
    private boolean isFirstItemFullShow() {

        //第一个条目可见
        if (listView.getChildCount() > 0 && listView.getFirstVisiblePosition() == 0) {

            return listView.getChildAt(0).getTop() == 0;

        }

        return false;

    }

}
